package com.hllxd.hism.star.basic.service.util;

import lombok.extern.slf4j.Slf4j;
import org.joda.time.DateTime;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.TimeZone;

/**
 * Created by BookThief on 2016/6/8.
 */
@Slf4j
public class TimeUtil {
	public static Integer remainSecondOfDay() {
		return 86400 - DateTime.now().getSecondOfDay();
	}

	/**
	 * 返回当前时间的哗啦啦时间戳,格式yyyyMMddHHmmss
	 *
	 * @return
	 */
	public static Long currentTimeHllDT() {
		DateTime now = DateTime.now();
		return currentTimeHllDT(now);

	}

	/**
	 * 返回当前时间的哗啦啦时间戳,格式yyyyMMdd
	 *
	 * @return
	 */
	public static Long currentTimeHllDT8() {
		DateTime now = DateTime.now();
		return currentTimeHllDT8(now);
	}

	public static Long DT8(int nday) {
		Calendar calendar1 = Calendar.getInstance();
		calendar1.add(Calendar.DATE, nday);
		SimpleDateFormat sdf1 = new SimpleDateFormat("yyyyMMdd");
		return Long.valueOf(sdf1.format(calendar1.getTime()));
	}

	/**
	 * 返回哗啦啦时间戳,格式yyyyMMddHHmmss
	 *
	 * @param time joda时间
	 * @return
	 */
	public static Long currentTimeHllDT(DateTime time) {
		return time.getYear() * 10000000000L +
				time.getMonthOfYear() * 100000000L +
				time.getDayOfMonth() * 1000000L +
				time.getHourOfDay() * 10000L +
				time.getMinuteOfHour() * 100L +
				time.getSecondOfMinute();
	}

	/**
	 * 返回哗啦啦时间戳,格式yyyyMMdd
	 *
	 * @param time joda时间
	 * @return
	 */
	public static Long currentTimeHllDT8(DateTime time) {
		return time.getYear() * 10000L +
				time.getMonthOfYear() * 100L +
				time.getDayOfMonth() * 1L;
	}

	/**
	 * 返回哗啦啦时间戳,格式yyyyMMddHHmmss
	 *
	 * @param timestamp unix时间戳
	 * @return
	 */
	public static Long currentTimeHllDT(long timestamp) {
		DateTime time = new DateTime(timestamp);
		return currentTimeHllDT(time);
	}

	public static DateTime hllDT2DateTime(Long hllDT) {
		return new DateTime(
				(int) (hllDT / 10000000000L),
				(int) ((hllDT / 100000000L) % 100),
				(int) ((hllDT / 1000000L) % 100),
				(int) ((hllDT / 10000L) % 100),
				(int) ((hllDT / 100L) % 100),
				(int) (hllDT % 100)
		);
	}

	/**
	 * 20160718121200 => '12:12'
	 *
	 * @param hllDT
	 * @return
	 */
	public static String getFormattedTimeFromHllDT(Long hllDT) {
		Long time = hllDT % 1000000;
		return String.format("%02d:%02d", time / 10000, time % 10000 / 100);
	}

	public static String getFormatted(Long hllDT) {
		Long time = hllDT % 1000000;
		return String.format("%02d:%02d", time / 10000, time % 10000 / 100);
	}

	/**
	 * 20160718121200 => 1212
	 *
	 * @param hllDT
	 * @return
	 */
	public static Long getDigitalTimeFromHllDT(Long hllDT) {
		Long time = hllDT % 1000000;
		return time / 100;
	}

	/**
	 * 20160718121200 => 20160718
	 *
	 * @param hllDT
	 * @return
	 */
	public static Long getDigitalDateFromHllDT(Long hllDT) {
		Long date = hllDT / 1000000;
		return date;
	}

	/**
	 * 20160718121200 => 2016-07-18 12:12
	 *
	 * @param hllDT
	 * @return
	 */
	public static String getDigitalDateTimeFromHllDT(Long hllDT) {
		return String.format("%04d-%02d-%02d %02d:%02d", hllDT / 10000000000L, hllDT % 10000000000L / 100000000,
				hllDT % 100000000 / 1000000, hllDT % 1000000 / 10000, hllDT % 10000 / 100);
	}

	/**
	 * 返回当前年份yyyy
	 *
	 * @return
	 */
//    public static int getYear() {
//        DateTime now = DateTime.now();
//        return now.getYear();
//    }
	public static String time(String format, long ms) {
		Date date = new Date(ms);
		SimpleDateFormat df = new SimpleDateFormat(format);
		df.setTimeZone(TimeZone.getTimeZone("GMT+8"));
		return df.format(date);
	}

	public static String time(long ms) {
		Date date = new Date(ms);
		SimpleDateFormat df = new SimpleDateFormat("yyyyMMddHHmmssSSS");
		df.setTimeZone(TimeZone.getTimeZone("GMT+8"));
		return df.format(date);
	}


	public static String dateFormat(Date date, String pattern) {
		DateFormat sdf = new SimpleDateFormat(pattern);
		return sdf.format(date);
	}

	public static Date dateParse(String date, String pattern) throws Exception {
		DateFormat sdf = new SimpleDateFormat(pattern);
		return sdf.parse(date);
	}

	/**
	 * 取得当前日期所在周的最后一天
	 *
	 * @param date
	 * @return
	 */
	public static Date getLastDayOfWeek(Date date) {
		Calendar calendar = Calendar.getInstance();
		calendar.setFirstDayOfWeek(Calendar.SUNDAY);
		calendar.setTime(date);
		calendar.set(Calendar.DAY_OF_WEEK, calendar.getFirstDayOfWeek() + 6); // Saturday
		return calendar.getTime();
	}

	public static String getDateBefore(Date date, int count, String pattern) {
		Calendar cal = Calendar.getInstance();
		cal.add(Calendar.DAY_OF_MONTH, count);
		return dateFormat(cal.getTime(), pattern);
	}

	public static String getInputDateBefore(Date date, int count, String pattern) {
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		cal.add(Calendar.DAY_OF_MONTH, count);
		return dateFormat(cal.getTime(), pattern);
	}

	public static String getDayAfter(Date date, int count, String pattern) {
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		int day = cal.get(Calendar.DATE);
		cal.set(Calendar.DATE, day + count);
		return dateFormat(cal.getTime(), pattern);
	}

	public static Date dateParse(String date) throws Exception {
		DateFormat df = DateFormat.getDateInstance();
		return df.parse(date);
	}

	public static String getWeekBegin(String dayID) {
		Date date;
		try {
			date = dateParse(dayID, "yyyyMMdd");
		} catch (Exception e) {
			return null;
		}
		int mondayPlus;
		Calendar cd = Calendar.getInstance();
		cd.setTime(date);
		// 获得今天是一周的第几天，星期日是第一天，星期二是第二天......
		int dayOfWeek = cd.get(Calendar.DAY_OF_WEEK); // 因为按中国礼拜一作为第一天所以这里减1
		if (dayOfWeek == 1) {//1是sunday
			mondayPlus = -6;
		} else {
			mondayPlus = 2 - dayOfWeek;
		}
		cd.add(Calendar.DATE, mondayPlus);
		Date monday = cd.getTime();
		return dateFormat(monday, "yyyyMMdd");
	}

	public static String getWeekEnd(String dayID) {
		Date date;
		try {
			date = dateParse(dayID, "yyyyMMdd");
		} catch (Exception e) {
			return null;
		}
		int mondayPlus;
		Calendar cd = Calendar.getInstance();
		cd.setTime(date);
		// 获得今天是一周的第几天，星期日是第一天，星期二是第二天......
		int dayOfWeek = cd.get(Calendar.DAY_OF_WEEK); // 因为按中国礼拜一作为第一天所以这里减1
		if (dayOfWeek == 1) {//1是sunday
			mondayPlus = 0;
		} else {
			mondayPlus = 7 + 1 - dayOfWeek;
		}
		//		GregorianCalendar currentDate = new GregorianCalendar();
		cd.add(Calendar.DATE, mondayPlus);
		Date monday = cd.getTime();
		return dateFormat(monday, "yyyyMMdd");
	}

	public static String getWeekBegin(int dayToAdd) {
		int mondayPlus;
		Calendar cd = Calendar.getInstance();
		cd.add(Calendar.DATE, dayToAdd);
		// 获得今天是一周的第几天，星期日是第一天，星期二是第二天......
		int dayOfWeek = cd.get(Calendar.DAY_OF_WEEK); // 因为按中国礼拜一作为第一天所以这里减1
		if (dayOfWeek == 1) {//1是sunday
			mondayPlus = -6;
		} else {
			mondayPlus = 2 - dayOfWeek;
		}
		cd.add(Calendar.DATE, mondayPlus);
		Date monday = cd.getTime();
		return dateFormat(monday, "yyyyMMdd");
	}

	public static String getWeekEnd(int dayToAdd) {
		int mondayPlus;
		Calendar cd = Calendar.getInstance();
		cd.add(Calendar.DATE, dayToAdd);
		// 获得今天是一周的第几天，星期日是第一天，星期二是第二天......
		int dayOfWeek = cd.get(Calendar.DAY_OF_WEEK); // 因为按中国礼拜一作为第一天所以这里减1
		if (dayOfWeek == 1) {//1是sunday
			mondayPlus = 0;
		} else {
			mondayPlus = 7 + 1 - dayOfWeek;
		}
		//		GregorianCalendar currentDate = new GregorianCalendar();
		cd.add(Calendar.DATE, mondayPlus);
		Date monday = cd.getTime();
		return dateFormat(monday, "yyyyMMdd");
	}

	/**
	 * 得到某年某周的第一天
	 *
	 * @param year
	 * @param week
	 * @return
	 */
	public static Date getFirstDayOfWeek(int year, int week) {
		week = week - 1;
		Calendar calendar = Calendar.getInstance();
		calendar.set(Calendar.YEAR, year);
		calendar.set(Calendar.MONTH, Calendar.JANUARY);
		calendar.set(Calendar.DATE, 1);

		Calendar cal = (Calendar) calendar.clone();
		cal.add(Calendar.DATE, week * 7);

		return getFirstDayOfWeek(cal.getTime());
	}

	/**
	 * 取得当前日期所在周的第一天
	 *
	 * @param date
	 * @return
	 */
	public static Date getFirstDayOfWeek(Date date) {
		Calendar calendar = Calendar.getInstance();
		calendar.setFirstDayOfWeek(Calendar.SUNDAY);
		calendar.setTime(date);
		calendar.set(Calendar.DAY_OF_WEEK, calendar.getFirstDayOfWeek()); // Sunday
		return calendar.getTime();
	}

	/**
	 * 得到某年某周的最后一天
	 *
	 * @param year
	 * @param week
	 * @return
	 */
	public static Date getLastDayOfWeek(int year, int week) {
		week = week - 1;
		Calendar calendar = Calendar.getInstance();
		calendar.set(Calendar.YEAR, year);
		calendar.set(Calendar.MONTH, Calendar.JANUARY);
		calendar.set(Calendar.DATE, 1);
		Calendar cal = (Calendar) calendar.clone();
		cal.add(Calendar.DATE, week * 7);

		return getLastDayOfWeek(cal.getTime());
	}






	/**
	 * 获取今天的日期
	 *
	 * @return YYYYMMDD 示例：20190120
	 */
	public static String getTodayDate() {
		Calendar cal = Calendar.getInstance();
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
		return format.format(cal.getTime());
	}

	/**
	 * 根据具体年份周数获取日期范围
	 *
	 * @param year
	 * @param week
	 * @return
	 */
	public static Long getWeekDays(int year, int week) {

//// 计算目标周数
//		if (week + targetNum > 52) {
//			year++;
//			week += targetNum - 52;
//		} else if (week + targetNum <= 0) {
//			year--;
//			week += targetNum + 52;
//		} else {
//			week += targetNum;
//		}

		SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
		Calendar cal = Calendar.getInstance();

// 设置每周的开始日期
		cal.setFirstDayOfWeek(Calendar.SUNDAY);

		cal.set(Calendar.YEAR, year);
		cal.set(Calendar.WEEK_OF_YEAR, week);

		cal.set(Calendar.DAY_OF_WEEK, cal.getFirstDayOfWeek());
		String beginDate = sdf.format(cal.getTime());


		return Long.parseLong(beginDate);
	}

	public static Long influxToTs(String ds) {
		DateFormat yyyyMMddHHmmss1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		try {
			Date date = yyyyMMddHHmmss1.parse(ds);
			return date.getTime();
		} catch (ParseException e) {
			return null;
		}
	}
	public static void main(String[] args) throws Exception {

	}
}
